import Toast from 'vant/lib/toast';

export default class CommonUtil {
  static TOAST_SHORT = 1000;

  /**
   * 判断是否存在方法
   * @param funcName
   * @returns {boolean}
   */
  static isExitsFunction(funcName) {
    try {
      if (typeof (eval(funcName)) === 'function') {
        return true;
      }
    } catch (e) {
    }
    return false;
  }


  static Toast(msg = '', time = CommonUtil.TOAST_SHORT) {
    msg = CommonUtil.E(msg, 'nothing');
    time = CommonUtil.E(time, CommonUtil.TOAST_SHORT);

    Toast({
      position: 'bottom',
      message: msg,
      duration: time
    });
  }

  /**
   * 对字符串进行处理
   * @param value
   * @param defaultValue
   * @returns {*}
   * @constructor
   */
  static E(value, defaultValue = '') {
    return CommonUtil.empty(value) ? defaultValue : value;
  }

  /**
   * 是否空
   * @param value
   * @returns {boolean}
   */
  static empty(value) {
    if (typeof (value) === 'undefined') return true;
    if (value === '') return true;
    if (value === null) return true;
    return false;
  }

  /**
   * 路由跳转
   * @param router  路由对象
   * @param path    跳转地址
   * @param query   传递参数
   */
  static router(router, path, query) {
    if (CommonUtil.empty(query)) query = {};
    router.push({
      path: path,
      query: query
    });
  }

  /**
   * 返回上一个界面
   * @param router
   */
  static routerBack(router) {
    router.history.go(-1);
  }


}
